package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.entity.*;
import com.wanlian.error.ErrorCode;
import com.wanlian.error.SuccessCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.QuestionServices;
import com.wanlian.service.UserServices;
import com.wanlian.vo.DeviceVo;
import com.wanlian.vo.Page;
import com.wanlian.vo.QuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionServices questionServices;

    @Autowired
    private UserServices userServices;

    /**
     * @return
     */
    @RequestMapping(value = "/qusans/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queAnsList(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = questionServices.queryAllUserQuestion(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 按条件查询反馈问题
     * @param data
     * @return
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listBycondition(@RequestBody String data) {
        PageCondition conditions = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = questionServices.queryMessage(conditions.getPage(), conditions.getConditions());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 添加反馈问题
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String add(@RequestBody String data,HttpSession session) {
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if(user==null){
            return new JsonResult().Serailze(false,ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        Question question = JsonStringUtil.readValue(data, Question.class);
        question.setEnable(true);
        question.setUserId(userId);
        ResultModel resultModel = questionServices.doAdd(question);
        resultModel.setMessage("添加成功");
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查询反馈问题类型
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/type/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String typeList(@RequestBody String data) {
        PageCondition conditions = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = questionServices.queryTypeList(conditions.getPage(), conditions.getConditions());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 按ID查询反馈问题类型
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/type/info", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String typeInfo(@RequestBody String data) {
        QuestionType questionType = JsonStringUtil.readValue(data, QuestionType.class);
        int id=questionType.getId();
        ResultModel resultModel=questionServices.queryTypeById(id);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 添加反馈问题类型
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/type/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String typeAdd(@RequestBody String data) {
        QuestionType questionType = JsonStringUtil.readValue(data, QuestionType.class);
        ResultModel resultModel = questionServices.doAdd(questionType);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 修改问题类型
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/type/upd", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String typeUpd(@RequestBody String data) {
        QuestionType questionType = JsonStringUtil.readValue(data, QuestionType.class);
        ResultModel resultModel = questionServices.doUpd(questionType);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 删除问题类型
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/type/del", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String typeDel(@RequestBody String data) {
        QuestionType questionType = JsonStringUtil.readValue(data, QuestionType.class);
        ResultModel resultModel = questionServices.doDel(questionType);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 回馈问题
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/answer/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String answerAdd(@RequestBody String data) {
        QuestionAnswer questionAnswer = JsonStringUtil.readValue(data, QuestionAnswer.class);
        ResultModel resultModel = questionServices.doAnswerAdd(questionAnswer);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 查询反馈
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/answer/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String answerList(@RequestBody String data) {
        PageCondition conditions = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = questionServices.queryQuestionAnswer(conditions.getPage(), conditions.getConditions());
        return new JsonResult().Serailze(resultModel);
    }
}
